package org.meetunexpectedly.controller;

import org.meetunexpectedly.entity.vo.BasePage;
import org.meetunexpectedly.holder.UserHolder;
import org.meetunexpectedly.limit.Limit;
import org.meetunexpectedly.entity.vo.Result;
import org.meetunexpectedly.entity.video.Video;
import org.meetunexpectedly.service.file.FileCloudService;
import org.meetunexpectedly.service.video.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: 视频控制类
 * @Author: pupil
 * @Date: 2024/08/17 下午 6:16
 */
@RestController
@RequestMapping("/meetunexpectedly/video")
public class VideoController {

    @Autowired
    private VideoService videoService;

    @Autowired
    private FileCloudService fileService;

    /**
     * 获取上传视频文件的token
     *
     * @return
     */
    @GetMapping("/token")
    public Result getToken() {
        return Result.ok().data(fileService.getToken());
    }

    /**
     * 发布视频/修改视频
     *
     * @param video 视频
     * @return
     */
    @PostMapping
    @Limit(limitCount = 5, time = 3600L, msg = "发布视频一小时内不可超过5次")
    public Result publishVideo(@RequestBody @Validated Video video) {
        videoService.publishVideo(video);
        return Result.ok().message("发布成功,请等待审核");
    }

    /**
     * 获取分类标签
     * @param types 标签id
     * @return
     */
    @GetMapping("/getClassifyLabels")
    public Result getClassifyLabels(@RequestParam(required = false) String types){
        return Result.ok().data(videoService.getClassifyLabelsById(types));
    }

    /**
     * 删除视频
     *
     * @param id 视频id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteVideo(@PathVariable Long id) {
        if (!videoService.deleteVideo(id)) {
            return Result.ok().message("删除失败");
        }
        return Result.ok().message("删除成功");
    }

    /**
     * 查看用户所管理的视频 -稿件管理
     *
     * @param basePage
     * @return
     */
    @GetMapping
    public Result getUserVideos(BasePage basePage) {
        return Result.ok().data(videoService.getUserVideos(basePage, UserHolder.get()));
    }

    /**
     * 点赞视频
     *
     * @param videoId 视频id
     * @return
     */
    @PostMapping("/thumbUp/{videoId}")
    public Result thumbUpVideo(@PathVariable Long videoId) {
        String msg = "已点赞";
        if (!videoService.thumbUpVideo(videoId)) {
            msg = "取消点赞";
        }
        return Result.ok().message(msg);
    }

    /**
     * 分享视频
     *
     * @param videoId 视频id
     * @param request 请求信息
     * @return
     */
    @PostMapping("/share/{videoId}")
    public Result shareVideo(@PathVariable Long videoId, HttpServletRequest request) {
        if(videoService.shareVideo(videoId, request)){
            return Result.ok().message("分享成功");
        }
        return Result.ok().message("已经分享过");
    }

    /**
     * 推送关注的人视频 拉模式   滚动分页
     *
     * @param lastTime 视频发布时间 滚动分页参数，首次为null，后续为上次的末尾视频时间
     * @return
     */
    @GetMapping("/follow/feed")
    public Result followFeed(@RequestParam(required = false) Long lastTime) {
        Long userId = UserHolder.get();
        return Result.ok().data(videoService.followFeed(userId, lastTime));
    }

    /**
     * 初始化关注流-推拉模式
     *
     * @return
     */
    @PostMapping("/init/follow/feed")
    public Result initFollowFeed() {
        Long userId = UserHolder.get();
        videoService.initFollowFeed(userId);
        return Result.ok();
    }

    /**
     * 获取当前审核队列状态
     *
     * @return
     */
    @GetMapping("/audit/queue/state")
    public Result getAuditQueueState() {
        return Result.ok().message(videoService.getAuditQueueState());
    }


    /**
     * 获取收藏夹下的视频
     *
     * @param favoriteId 收藏夹id
     * @return
     */
    @GetMapping("/favorites/{favoriteId}")
    public Result getFavoritesVideos(@PathVariable Long favoriteId) {
        return Result.ok().data(videoService.getFavoritesVideos(favoriteId));
    }

    /**
     * 收藏视频
     *
     * @param favoriteId 收藏夹id
     * @param videoId     视频id
     * @return
     */
    @PostMapping("/favorites/{favoriteId}/{videoId}")
    public Result favoritesVideo(@PathVariable Long favoriteId, @PathVariable Long videoId) {
        return Result.ok().message(videoService.favoritesVideo(favoriteId, videoId) ? "收藏成功" : "取消收藏");
    }

    /**
     * 获取用户的浏览记录
     * @param basePage 基础分页信息
     * @return
     */
    @GetMapping("/history")
    public Result getHistory(BasePage basePage){
        return Result.ok().data(videoService.getHistory(basePage));
    }

    /**
     * 添加用户视频浏览记录
     * @param videoId 视频id
     * @return
     */
    @PostMapping("/history/{videoId}")
    public Result addHistory(@PathVariable Long videoId){
        videoService.addHistory(videoId,UserHolder.get());
        return Result.ok();
    }

}
